﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Home.Mobilesales.Common;
using Home.MobileSales.Common.Components;
using Home.MobileSales.Common.Entities;
using Home.MobileSales.Sales.Interface;
using Home.MobileSales.Common.Models;

namespace Home.MobileSales.Sales.Presenter
{
    class DailyVisitsPresenter
    {
        private DateTime _visitDate = DateTime.Now;
        private IDailyVisitsView _view = null;
        private VisitRepository _dbVisit = null;

        public DailyVisitsPresenter(IDailyVisitsView view, DateTime dateTime)
        {
            try
            {
                _dbVisit = DataManager.GetInstance().Visists;
                _visitDate = dateTime;
                _view = view;
                _view.DataSource = _dbVisit.GetVisitsByDate(_visitDate,false);

                _view.AddVisit += AddVisitDialog;
                _view.DeleteVisit += DeleteDailyVisit;
                _view.CreateFromShedule += CreateVisitsFromShedule;
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }

        }

        public void CreateVisitsFromShedule(object sender, EventArgs e)
        {
            try
            {
                _dbVisit.LoadFromDate(_visitDate);
                _view.DataSource = _dbVisit.GetVisitsByDate(_visitDate, false);
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void CancelAddVisitDialog(object sender, EventArgs e)
        {
            _view.CloseAddVisitDialog();
        }

        public void AddVisit(object sender, EventArgs e)
        {

            try
            {
                AddVisitDialog addVisitDialog = (AddVisitDialog)sender;
                Visit visit = new Visit();
                visit.DateVisit = _visitDate.ToString();
                visit.BuyPoint = ((BuyPoint)addVisitDialog.SelectedBuyPoint);
                _dbVisit.CreateVisit(visit);
                _view.DataSource = _dbVisit.GetVisitsByDate(_visitDate, false);
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void AddVisitDialog(object sender, EventArgs e)
        {
            try
            {
                AddVisitDialog addVisitDialog = new AddVisitDialog();
                addVisitDialog.OK += AddVisit;
                addVisitDialog.Cancel += CancelAddVisitDialog;
                _view.ShowAddVisitDialog(addVisitDialog);
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void DeleteDailyVisit(object sender, EventArgs e)
        {
            try
            {
                Visit currentVisit = ((List<Visit>)_view.DataSource)[(int)_view.Current];
                _dbVisit.DeleteVisit(currentVisit.IdVisit);
                _view.DataSource = _dbVisit.GetVisitsByDate(_visitDate, false);
            }

            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }
    }
}
